package cn.admobiletop.adsuyi.adapter.gdt.data;

import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.ads.nativ.NativeExpressMediaListener;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.pi.NEADVI;
import com.qq.e.comm.util.AdError;

import org.json.JSONObject;

import java.lang.reflect.Field;

import cn.admobiletop.adsuyi.ad.data.ADSuyiNativeExpressAdInfo;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeVideoListener;
import cn.admobiletop.adsuyi.ad.material.ADSuyiMaterialNativeExpressAd;
import cn.admobiletop.adsuyi.ad.widget.ADSuyiInterceptContainer;
import cn.admobiletop.adsuyi.adapter.gdt.manager.DownloadConfirmHelper;
import cn.admobiletop.adsuyi.util.ADSuyiViewUtil;

/**
 * @author ciba
 * @description 模板信息流广告内容对象
 * @date 2020/4/8
 */
public class NativeExpressAdInfo extends BaseAdInfo<ADSuyiNativeAdListener, NativeExpressADView>
        implements ADSuyiNativeExpressAdInfo, NativeExpressMediaListener {
    private ADSuyiNativeVideoListener adSuyiNativeVideoListener;

    public NativeExpressAdInfo(String platformPosId) {
        super(platformPosId);
    }

    @Override
    public boolean isNativeExpress() {
        return true;
    }

    @Override
    public boolean isVideo() {
        return getAdapterAdInfo() != null
                && getAdapterAdInfo().getBoundData() != null
                && getAdapterAdInfo().getBoundData().getAdPatternType() == AdPatternType.NATIVE_VIDEO;
    }

    @Override
    public void setVideoListener(ADSuyiNativeVideoListener adSuyiNativeVideoListener) {
        if (isVideo()) {
            this.adSuyiNativeVideoListener = adSuyiNativeVideoListener;
            getAdapterAdInfo().setMediaListener(this);
        }
    }

    @Override
    public View getNativeExpressAdView(@NonNull ViewGroup container) {
        ADSuyiViewUtil.releaseClickTouchListener(container);
        if (getAdapterAdInfo() != null) {
            return getAdapterAdInfo();
        }
        return null;
    }

    @Override
    public void render(@NonNull ViewGroup viewGroup) {
        ADSuyiViewUtil.releaseClickTouchListener(viewGroup);
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().render();
        }
    }

    @Override
    public void onVideoInit(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onVideoLoading(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onVideoCached(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onVideoReady(NativeExpressADView nativeExpressADView, long l) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoLoad(this);
        }
    }

    @Override
    public void onVideoStart(NativeExpressADView nativeExpressADView) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoStart(this);
        }
    }

    @Override
    public void onVideoPause(NativeExpressADView nativeExpressADView) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoPause(this);
        }
    }

    @Override
    public void onVideoComplete(NativeExpressADView nativeExpressADView) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoComplete(this);
        }
    }

    @Override
    public void onVideoError(NativeExpressADView nativeExpressADView, AdError adError) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoError(this, new ADSuyiError(adError.getErrorCode(), adError.getErrorMsg()));
        }
    }

    @Override
    public void onVideoPageOpen(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onVideoPageClose(NativeExpressADView nativeExpressADView) {}

    @Override
    public void onCloseClick(View view) {
        if (getAdListener() != null) {
            getAdListener().onAdClose(NativeExpressAdInfo.this);
        }
    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        adSuyiNativeVideoListener = null;
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().destroy();
            setAdapterAdInfo(null);
        }
    }
}
